﻿using IOA.MES.Business;
using IOA.MES.DataAccess.Enum;
using IOA.MES.DataAccess.VModel;
using IOA.MES.PdaApi.Models.Request.Inbound;
using IOA.MES.PdaApi.Models.Response.Inbound;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace IOA.MES.PdaApi.Controllers
{
    /// <summary>
    /// 入库管理
    /// </summary>
    public class InboundController : ApiController
    {
        /// <summary>
        /// 入库请求
        /// </summary>
        [HttpPost]
        public ResultJson<InboundResponse> Inbound([FromBody] InboundRequest request)
        {
            var user = BaseEnv.User;
            var inbound = new VInbound
            {
                CreateBy = user.PkId,
                InboundType = InboundType.ProductInbound,
                WarehouseCode = BaseEnv.WarehouseCode,
                WarehouseName = BaseEnv.Warehouse?.WarehouseName,
            };
            var packs = request.InboundItems.Select(_ => new VInboundProduct
            {
                BarCode = _.PackNo,
                CreateBy = user.PkId,
                InventoryType = InventoryType.ProductPack,
                Location = _.Location,
                ReceiveQuantity = 1
            }).ToList();
            new InboundMgr().Inbound(inbound, packs);
            return new ResultJson<InboundResponse>();
        }

        /// <summary>
        /// 查询入库记录
        /// </summary>
        [HttpPost]
        public ResultJson<QueryInboundsResponse> QueryInbounds([FromBody] QueryInboundsRequest request)
        {
            var total = 0;
            var inbounds = new InboundMgr().QueryInbounds(request.PageIndex, request.PageSize, out total, BaseEnv.WarehouseCode, InboundType.ProductInbound, null, null, BaseEnv.User.PkId);
            return new ResultJson<QueryInboundsResponse>(new QueryInboundsResponse
            {
                Inbounds = inbounds.Select(_ => new Inbound
                {
                    WarehouseCode = _.WarehouseCode,
                    WarehouseName = _.WarehouseName,
                    Count = _.Products.Count,
                    InboundBy = _.CNCreateBy,
                    InboundNo = _.InboundNo,
                    InboundTypeNo = _.InboundTypeNo,
                    InboundTime = _.CreateTime,
                    Packs = _.Products.Select(p => new Models.Response.Product.PackItem
                    {
                        AreaCode = p.AreaCode,
                        AreaName = p.AreaName,
                        Quantity = p.ReceiveQuantity,
                        Location = p.Location,
                        PackBatchNo = p.BatchNo,
                        PackNo = p.BarCode,
                        ProductCode = p.ProductCode,
                        ProductName = p.ProductName
                    }).ToList()
                }).ToList()
            }, total);
        }

        /// <summary>
        /// 获取入库记录详情
        /// </summary>
        [HttpPost]
        public ResultJson<GetInboundResponse> GetInbound([FromBody] GetInboundRequest request)
        {
            var inbound = new InboundMgr().GetInbound(request.InboundNo);
            return new ResultJson<GetInboundResponse>(new GetInboundResponse
            {
                Inbound = inbound == null ? null : new Inbound
                {
                    InboundBy = inbound.CNCreateBy,
                    Count = inbound.Products.Count,
                    InboundNo = inbound.InboundNo,
                    InboundTime = inbound.CreateTime,
                    WarehouseCode = inbound.WarehouseCode,
                    WarehouseName = inbound.WarehouseName,
                    Packs = inbound.Products.Select(p => new Models.Response.Product.PackItem
                    {
                        AreaCode = p.AreaCode,
                        AreaName = p.AreaName,
                        Quantity = p.ReceiveQuantity,
                        Location = p.Location,
                        PackBatchNo = p.BatchNo,
                        PackNo = p.BarCode,
                        ProductCode = p.ProductCode,
                        ProductName = p.ProductName
                    }).ToList()
                }
            });
        }
    }
}
